/***************************************************************************//**
 * @file MSc_Function.cpp
 * @author  Marek M. Cel <marekcel@mscsim.org>
 *
 * @section LICENSE
 *
 * Copyright (C) 2014 Marek M. Cel
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 ******************************************************************************/
#ifndef MSC_FUNCTION_CPP
#define MSC_FUNCTION_CPP
#endif

////////////////////////////////////////////////////////////////////////////////

#include <cmath>

#include <utils/MSc_Argument.h>
#include <utils/MSc_Exception.h>
#include <utils/MSc_Function.h>

////////////////////////////////////////////////////////////////////////////////

using namespace std;

////////////////////////////////////////////////////////////////////////////////

MSc_Function::MSc_Function() :
    m_argCount ( 0 )
{
    m_arguments.clear();

    check();
}

////////////////////////////////////////////////////////////////////////////////

MSc_Function::~MSc_Function()
{
    for ( size_t i = 0; i < m_argCount; i++ )
    {
        if ( m_arguments[ i ] ) delete m_arguments[ i ]; m_arguments[ i ] = 0;
    }

    m_arguments.clear();
}

////////////////////////////////////////////////////////////////////////////////

void MSc_Function::addArgument( MSc_Argument *arg )
{
    m_arguments.push_back( arg );

    check();
}

////////////////////////////////////////////////////////////////////////////////

double MSc_Function::getValue() const
{
    if ( m_argValid )
    {
        double result = m_arguments[ 0 ]->getValue();

        switch ( m_operation )
        {
        case Sum:
            for ( size_t i = 1; i < m_argCount; i++ )
            {
                result += m_arguments[ i ]->getValue();
            }
            break;

        case Difference:
            result -= m_arguments[ 1 ]->getValue();
            break;

        case Product:
            for ( size_t i = 1; i < m_argCount; i++ )
            {
                result *= m_arguments[ i ]->getValue();
            }
            break;

        case Quotient:
            result /= m_arguments[ 1 ]->getValue();
            break;

        case Power:
            result = pow( result, m_arguments[ 1 ]->getValue() );
            break;

        case Exponent:   result = exp  ( result ); break;
        case Absolute:   result = fabs ( result ); break;
        case Sine:       result = sin  ( result ); break;
        case Cosine:     result = cos  ( result ); break;
        case Tangent:    result = tan  ( result ); break;
        case Arcsine:    result = asin ( result ); break;
        case Arccosine:  result = acos ( result ); break;
        case Arctangent: result = atan ( result ); break;

        case Arctangent2:
            result = atan2( result, m_arguments[ 1 ]->getValue() );
            break;

        case Minimum:
            for ( size_t i = 1; i < m_argCount; i++ )
            {
                result = min( result, m_arguments[ i ]->getValue() );
            }
            break;

        case Maximum:
            for ( size_t i = 1; i < m_argCount; i++ )
            {
                result = max( result, m_arguments[ i ]->getValue() );
            }
            break;

        case Average:
            for ( size_t i = 1; i < m_argCount; i++ )
            {
                result += m_arguments[ i ]->getValue();
            }
            result /= (double)m_argCount;
            break;
        }

        return result;
    }
    else
    {
        MSc_Exception e;

        e.setType( MSc_Exception::UnknownException );
        e.setFile( __FILE__ );
        e.setLine( __LINE__ );
        e.setInfo( "Invalid arguments count." );

        throw e;
    }

    return std::numeric_limits< double >::quiet_NaN();
}

////////////////////////////////////////////////////////////////////////////////

void MSc_Function::setOperation( Operation operation )
{
    m_operation = operation;

    check();
}

////////////////////////////////////////////////////////////////////////////////

void MSc_Function::check()
{
    m_argCount = m_arguments.size();
    m_argValid = false;

    switch ( m_operation )
    {
        case Sum:         m_argValid = ( m_argCount >= 2 ); break;
        case Difference:  m_argValid = ( m_argCount == 2 ); break;
        case Product:     m_argValid = ( m_argCount >= 2 ); break;
        case Quotient:    m_argValid = ( m_argCount == 2 ); break;
        case Power:       m_argValid = ( m_argCount == 2 ); break;
        case Exponent:    m_argValid = ( m_argCount == 1 ); break;
        case Absolute:    m_argValid = ( m_argCount == 1 ); break;
        case Sine:        m_argValid = ( m_argCount == 1 ); break;
        case Cosine:      m_argValid = ( m_argCount == 1 ); break;
        case Tangent:     m_argValid = ( m_argCount == 1 ); break;
        case Arcsine:     m_argValid = ( m_argCount == 1 ); break;
        case Arccosine:   m_argValid = ( m_argCount == 1 ); break;
        case Arctangent:  m_argValid = ( m_argCount == 1 ); break;
        case Arctangent2: m_argValid = ( m_argCount == 2 ); break;
        case Minimum:     m_argValid = ( m_argCount >= 2 ); break;
        case Maximum:     m_argValid = ( m_argCount >= 2 ); break;
        case Average:     m_argValid = ( m_argCount >= 2 ); break;
    }
}
